package cn.bupt.adt.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ScheduledThreadPoolExecutor;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.bupt.adt.constant.Constants;
import cn.bupt.adt.dao.autogenerate.FirstLevelInspectionItemMapper;
import cn.bupt.adt.dao.autogenerate.InspectOrderMapper;
import cn.bupt.adt.dao.autogenerate.InspectOrderViewMapper;
import cn.bupt.adt.dao.autogenerate.InspectViewMapper;
import cn.bupt.adt.dao.autogenerate.InspetionMapper;
import cn.bupt.adt.dao.autogenerate.InstitutionAppUserMapper;
import cn.bupt.adt.dao.autogenerate.SecondLevelViewMapper;
import cn.bupt.adt.entity.autogenerate.FirstLevelInspectionItem;
import cn.bupt.adt.entity.autogenerate.FirstLevelInspectionItemExample;
import cn.bupt.adt.entity.autogenerate.InspectOrder;
import cn.bupt.adt.entity.autogenerate.InspectOrderExample;
import cn.bupt.adt.entity.autogenerate.InspectOrderView;
import cn.bupt.adt.entity.autogenerate.InspectOrderViewExample;
import cn.bupt.adt.entity.autogenerate.InspectView;
import cn.bupt.adt.entity.autogenerate.InspectViewExample;
import cn.bupt.adt.entity.autogenerate.Inspetion;
import cn.bupt.adt.entity.autogenerate.InstitutionAppUser;
import cn.bupt.adt.entity.autogenerate.SecondLevelView;
import cn.bupt.adt.entity.autogenerate.SecondLevelViewExample;
import cn.bupt.adt.model.InspectAllOrderModel;
import cn.bupt.adt.model.InspectLevelModel;
import cn.bupt.adt.model.InspectOrderModel;
import cn.bupt.adt.service.IInstitutionAppUserService;
import cn.bupt.adt.service.IInstitutionService;
import cn.bupt.adt.service.IinspectOrderService;
import cn.bupt.adt.util.BeanUtil;
import cn.bupt.adt.util.SendSMSUtil;

@Service
public class InspectOrderServiceImpl implements IinspectOrderService{
	
	@Autowired
	InspectOrderViewMapper inspectOrderViewMapper;
	
	@Autowired
	IInstitutionAppUserService institutionAppUserService;
	
	@Autowired
	InstitutionAppUserMapper institutionAppUserMapper;
	
	@Autowired
	SecondLevelViewMapper secondLevelViewMapper;
	
	@Autowired
	FirstLevelInspectionItemMapper firstLevelInspectionItemMapper;
	
	@Autowired
	InspectViewMapper inspectViewMapper;
	
	@Autowired
	InspetionMapper inspectionMapper;
	
	@Autowired
	InspectOrderMapper inspectOrderMapper;
	
	@Autowired
	IInstitutionService institutionService;
//	@Autowired
//	FirstLevelInspectionItemExample firstLevelInspectionItemExample;
	
	@Override
	public List<InspectOrderModel> getAllInspectOrders() {   
		List<InspectOrderView> inspects = inspectOrderViewMapper.selectByExample(new InspectOrderViewExample());
		List<InspectOrderModel> res = new ArrayList<>(inspects.size());
		for(InspectOrderView inspect:inspects) {
			if(inspect.getStatus() == null)
				continue;
			InspectOrderModel dm = new InspectOrderModel();
			try {
				BeanUtil.fatherToChild(inspect, dm);
			} catch (Exception e) {
				e.printStackTrace();
			}
			res.add(dm);
		}
		return res;
	}
	
	@Override
	public List<InspectAllOrderModel> getAllInspectItemsOrders() {   
		List<InspectOrder> inspects = inspectOrderMapper.selectByExample(new InspectOrderExample());
		List<InspectAllOrderModel> res = new ArrayList<>(inspects.size());
		for(InspectOrder inspect:inspects) {
			if(inspect.getStatus() == null)
				continue;
			InspectAllOrderModel dm = new InspectAllOrderModel();
			try {
				BeanUtil.fatherToChild(inspect, dm);
			} catch (Exception e) {
				e.printStackTrace();
			}
			res.add(dm);
		}
		return res;
	}

	@Override
	public List<InspectOrderModel> getOrderByName(String content) {
		InspectOrderViewExample ex = new InspectOrderViewExample();
		ex.createCriteria().andNameLike("%"+content+"%");
		List<InspectOrderView> inspects = inspectOrderViewMapper.selectByExample(ex);
		return transferModel(inspects);
	}

	private List<InspectOrderModel> transferModel(List<InspectOrderView> inspects){
		List<InspectOrderModel> res = new ArrayList<>(inspects.size());
		for(InspectOrderView ispect:inspects) {
			if(ispect.getStatus() == null)
				continue;
			InspectOrderModel dm = new InspectOrderModel();
			try {
				BeanUtil.fatherToChild(ispect, dm);
			} catch (Exception e) {
				e.printStackTrace();
			}

		res.add(dm);
		}
		return res;
	}

	@Override
	public List<InspectOrderModel> getOrderByNumber(String content) {
		InspectOrderViewExample ex = new InspectOrderViewExample();
		ex.createCriteria().andNumEqualTo(content);
		List<InspectOrderView> inspects = inspectOrderViewMapper.selectByExample(ex);
		return transferModel(inspects);	
	}
	
	@Override
	public InspectOrderView getOrderViewById(int id) {
		InspectOrderViewExample ex = new InspectOrderViewExample();
		ex.createCriteria().andIdEqualTo(id);
		return inspectOrderViewMapper.selectByExample(ex).get(0);
	}
	
	@Override
	public InspectOrder getOrderById(int id) {
		return inspectOrderMapper.selectByPrimaryKey(id);
	}
	
	@Override
	public InspectView getInspectViewById(int id) {
		InspectViewExample ex = new InspectViewExample();
		ex.createCriteria().andIdEqualTo(id);
		return inspectViewMapper.selectByExample(ex).get(0);
	}
	
	@Override
	public boolean addOrder(InspectOrder order) {
//		ScheduledThreadPoolExecutor timer = new ScheduledThreadPoolExecutor(1);
		//添加订单
		int i = inspectOrderMapper.insert(order);
		InstitutionAppUser ins = institutionAppUserMapper.selectByPrimaryKey(order.getInstitutionId());
		int j = institutionAppUserMapper.updateByPrimaryKeySelective(ins);
		return i == 1 && j == 1;
	}
	
	
	@Override
	public boolean takeOrder(InspectOrder order,Integer inspectionId) {
		try {
			order.setStatus(1);
			order.setInspectionId(inspectionId);
			InspectOrderExample ex = new InspectOrderExample();
			ex.createCriteria().andIdEqualTo(order.getId());
			ex.createCriteria().andStatusEqualTo(order.getStatus());
			int rs = inspectOrderMapper.updateByExampleSelective(order, ex);
			
			if (rs != 0) {
				SendSMSUtil sendSms = new SendSMSUtil();
				sendSms.SendSMS(institutionService.getInstitutionByAppId(getOrderById(order.getId()).getInstitutionId()).getPhone() , new String[]{String.valueOf(order.getId())}, Constants.SMS_CHECK_JIEDAN);
				
				return true;
			} else {
				return false;
			}
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}
	
	@Override
	public boolean updateOrder(InspectOrderView order) {
		try {
			
			InspectOrderViewExample ex = new InspectOrderViewExample();
			ex.createCriteria().andIdEqualTo(order.getId());
			ex.createCriteria().andStatusEqualTo(order.getStatus());
			int rs = inspectOrderViewMapper.updateByExampleSelective(order, ex);
			if (rs != 0) {
				if (order.getStatus()==2) {
					SendSMSUtil sendSms = new SendSMSUtil();
					System.out.println("0820机构手机号为"+institutionService.getInstitutionByAppId(getOrderById(order.getId()).getInstitutionId()).getPhone());
					sendSms.SendSMS(institutionService.getInstitutionByAppId(getOrderById(order.getId()).getInstitutionId()).getPhone() , new String[]{String.valueOf(order.getId())}, Constants.SMS_CHECK_EXAP);				
				}
				if (order.getStatus()==3) {
					System.out.println("0820机构手机号为"+(getOrderById(order.getId()).getInstitutionId()));
					SendSMSUtil sendSms = new SendSMSUtil();
					sendSms.SendSMS(institutionService.getInstitutionByAppId(getOrderById(order.getId()).getInstitutionId()).getPhone() , new String[]{String.valueOf(order.getId())}, Constants.SMS_CHECK_OVER);				
				}
				return true;
			} else {
				return false;
			}
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}
	
	@Override
	public List<SecondLevelView> getSecondLevelByfirstLevelId(Integer firstLevelId) {
		SecondLevelViewExample ex = new SecondLevelViewExample();
		ex.createCriteria().andFirstLevelIdEqualTo(firstLevelId);
		return secondLevelViewMapper.selectByExample(ex);
	}
	
	@Override
	public List<FirstLevelInspectionItem> getAllFirstLevelInspectionItem() {   
		return firstLevelInspectionItemMapper.selectByExample(new FirstLevelInspectionItemExample());
	}
	
	@Override
	public List<InspectLevelModel> getLevelInspectionItem() {   
		// TODO Auto-generated method stub
		List<InspectLevelModel> TypeList = null;
		List<InspectLevelModel> subTypeModels = null;
		Integer parentId = -1;// 一级菜单类型
		TypeList = this.getSubType(parentId);
		for (InspectLevelModel TypeModel : TypeList) {
			subTypeModels = this.getSubType(TypeModel.getId());
			TypeModel.setSecondLevelList(subTypeModels);
		}
		return TypeList;
	}
	
	/**
	 * 查找子类型
	 */
	@Override
	public List<InspectLevelModel> getSubType(Integer parentId) {
		SecondLevelViewExample TypeExample = new SecondLevelViewExample();
		SecondLevelViewExample.Criteria criteria = TypeExample.createCriteria();
		criteria.andFirstLevelIdEqualTo(parentId);
		List<SecondLevelView> TypeList = secondLevelViewMapper
				.selectByExample(TypeExample);
		List<InspectLevelModel> TypeModelList = new ArrayList<InspectLevelModel>();
		for (SecondLevelView Type : TypeList) {
			InspectLevelModel TypeModel = new InspectLevelModel();
			TypeModel.setId(Type.getId());
			TypeModel.setParentName(Type.getFirstLevelName());
			TypeModel.setSecondLevelName(Type.getSecondLevelName());
			TypeModel.setFirstLevelName(Type.getFirstLevelName());
			TypeModel.setFirstLevelInspectionItemId(Type.getFirstLevelInspectionItemId());
			TypeModel.setFirstLevelId(Type.getFirstLevelId());
			TypeModelList.add(TypeModel);
		}
		return TypeModelList;
	}
	
	@Override
	public List<InspectOrderView> getAllInspectItemOrders(String secondLevelId) {
		InspectOrderViewExample ex = new InspectOrderViewExample();
		ex.createCriteria().andSecondLevelInspectionItemIdEqualTo( Integer.parseInt(secondLevelId));
		return inspectOrderViewMapper.selectByExample(new InspectOrderViewExample());
	}
	
	@Override
	public List<InspectOrderView> getAllInspectItemOrders() {
		return inspectOrderViewMapper.selectByExample(new InspectOrderViewExample());
	}
	
	@Override
	public List<InspectOrder> getAllInspectItemOrdersInInspectOrder() {
		return inspectOrderMapper.selectByExample(new InspectOrderExample());
	}
	
	@Override
	public List<InspectView> getAllInspectItems() {
		return inspectViewMapper.selectByExample(new InspectViewExample());
	}
	
	@Override
	public List<InspectView> getInspectItemsBySecondLevel(String secondLevelId) {
		InspectViewExample ex = new InspectViewExample();
		ex.createCriteria().andSecondLevelInspectionItemIdEqualTo(secondLevelId);
		return inspectViewMapper.selectByExample(ex);
	}
	
	@Override
	public List<InspectView> getInspectItemsByFirstLevel(String firstLevelId) {
		InspectViewExample ex = new InspectViewExample();
		ex.createCriteria().andFirstLevelInspectionItemIdEqualTo(Integer.parseInt(firstLevelId));
		return inspectViewMapper.selectByExample(ex);
	}
	
	@Override
	public Inspetion getInspetionByChecckId(Integer checkId){
		return inspectionMapper.selectByPrimaryKey(checkId);
	}

	@Override
	public boolean addOrder(List<InspectOrder> orders) {
		boolean flag = true;
		for(InspectOrder order:orders)
			flag &= addOrder(order);
		return flag;
	}
}
